package com.zj.core.base

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.View
import android.widget.LinearLayout
import com.dialogutils.ToastUtil
import com.zj.core.bean.*
import com.zj.core.net.NetPresenter
import com.zj.core.net.NetValueCallBackIml
import com.zj.core.tools.VoiceFormatTools

/**
 * 日期：2019年6月21日
 * 公司：北京子杰宝贝科技有限公司
 * 地址：北京市朝阳区朝阳农场
 * 开发者：达选文
 *
 * 主体功能：数据录入界面网络请求
 */
@SuppressLint("Registered")
open class BaseNetBeanActivity : TitleBarActivity() {
    var mTitleArr: ArrayList<TemplateTitleBean.Rows>? = null           //模板关键字列表

    var mDialogType: Int = 0

    var formList = ""                    //表单列表
    var mStatuMap:HashMap<String,String> ?=null
    private var mFormListStr = ""                //获取表单内容关键字
    private var mHospitalStr = ""                //住院状态关键字
    var mDrugsList=""                    //药品名称列表
    var mLevelType=""

    override fun onCreate(groupView: LinearLayout): View? {
        return null
    }

    override fun initView() {}

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        mTitleArr = ArrayList()

        /**
         * 获取表单数据
         */
        NetPresenter().getFormList(object : NetValueCallBackIml {
            override fun onSubscribe(data: Any?) {}
            override fun onError() {}
            override fun onComplete() {}

            override fun onNext(values: Any?, bean: Any?) {
                (values as List<FormListBean.Dat.Rows>).apply {
                    for (i in this) {
                        if (i.hospitalid.equals(mHospitalID) && i.departmentid.equals(mDepartmentID)) {
                            formList = i.configform.toString()
                        }
                    }
                }
            }
        }, mHospitalID, mDepartmentID)

        /**
         * 模板标题列表查询
         *
         * @param rowsBean
         */
        NetPresenter().getTemplateTitle(object : NetValueCallBackIml {
            override fun onNext(values: Any?, bean: Any?) {
                mTitleArr = values as ArrayList<TemplateTitleBean.Rows>
            }

            override fun onError() {}
            override fun onComplete() {}
            override fun onSubscribe(data: Any?) {}
        }, mDepartmentID, mHospitalID)


        /**
         * 获取表单内容关键字
         */
        NetPresenter().getFormKey(object : NetValueCallBackIml {
            override fun onNext(values: Any?, bean: Any?) {
                values?.let {
                    NetPresenter().getHospitalStatusKey(
                        mHospitalStatu,
                        mHospitalID,
                        values as ArrayList<FormKeySelectBean.DatBean.RowsBean>?
                    )
                }
            }

            override fun onError() {
                ToastUtil.showShort(BaseApplication.context, "表单关键字获取失败")
            }

            override fun onSubscribe(data: Any?) {}
            override fun onComplete() {}
        }, mDepartmentID, mHospitalID)

        /**
         * 获取药品名称列表
         *
         * @param iml
         * @param imei
         */
        NetPresenter().getDrugsList(object: NetValueCallBackIml {
            override fun onSubscribe(data: Any?) {}
            override fun onNext(values: Any?, bean: Any?) {
                (values as List<DrugsListBean.Dat.Rows>).apply {
                    for(indexRow in this){
                        val mDraugsName=indexRow.drugsname.toString()
                        mDrugsList = if(mDrugsList == ""){
                            mDraugsName
                        }else{
                            "$mDrugsList|$mDraugsName"
                        }
                    }
                    drugsNameResult(mDrugsList)
                }
            }
            override fun onError() {
                ToastUtil.showShort(BaseApplication.context, "药品名称列表获取失败")
            }
            override fun onComplete() {}
        })

        /**
         * 获取护理等级
         */
        NetPresenter().getNurseLevel(object : NetValueCallBackIml {
            override fun onSubscribe(data: Any?) {}
            override fun onError() {
                ToastUtil.showShort(BaseApplication.context, "护理等级获取失败")
            }
            override fun onComplete() {}
            override fun onNext(values: Any?, bean: Any?) {
                (values as List<NurseLevelBean.DatBean.RowsBean>).apply {
                    for (i in this) {
                        if (i.hospitalid.equals(mHospitalID)){
                            mLevelType=i.leveltype as String
                        }
                    }
                }
            }
        },mHospitalID)
    }

    /**
     * 获取医院状态,且格式化数据
     */
    private val mHospitalStatu = object : NetValueCallBackIml {
        override fun onNext(values: Any?, bean: Any?) {
            formatBean(
                values as ArrayList<HospitalStatuBean.DatEntity.RowsEntity>,
                bean as ArrayList<FormKeySelectBean.DatBean.RowsBean>
            )
        }

        override fun onError() {
            ToastUtil.showShort(BaseApplication.context, "表单关键字获取失败")
        }

        override fun onSubscribe(data: Any?) {}
        override fun onComplete() {}
    }

    /**
     * 数据标准化处理
     */
    private fun formatBean(
        statuBean: ArrayList<HospitalStatuBean.DatEntity.RowsEntity>?,
        formBean: ArrayList<FormKeySelectBean.DatBean.RowsBean>?
    ) {
        mStatuMap= HashMap()
        formBean?.let {
            for (detailBean in it){
                val mStrA = detailBean.titlea
                val mStrB = detailBean.titleb

                if (mStrA != "" && mStrB != "") {
                    if (mStrA != mStrB) {
                        if (mStrA!!.length >= mStrB!!.length) {
                            mFormListStr = "$mFormListStr$mStrA|$mStrB|"
                        } else {
                            mFormListStr = "$mFormListStr$mStrB|$mStrA|"
                        }
                    } else {
                        mFormListStr = "$mFormListStr$mStrA|"
                    }
                } else if (mStrA == "" && mStrB != "") {
                    mFormListStr = "$mFormListStr$mStrB|"
                } else if (mStrA != "" && mStrB == "") {
                    mFormListStr = "$mFormListStr$mStrA|"
                }
            }
        }

        statuBean?.let {
            for (hospitalBean in statuBean) {
                mHospitalStr = mHospitalStr + hospitalBean.statusstr + "|"
                mStatuMap?.put(hospitalBean.statusstr.toString(),hospitalBean.conversion.toString())
            }
        }

        if (mFormListStr.substring(mFormListStr.length - 1, mFormListStr.length).equals("|")) {
            mFormListStr = mFormListStr.substring(0, mFormListStr.length - 1)
        }

        if (mHospitalStr.substring(mHospitalStr.length - 1, mHospitalStr.length).equals("|")) {
            mHospitalStr = mHospitalStr.substring(0, mHospitalStr.length - 1)
        }
        patternKeyResult(mHospitalStr, VoiceFormatTools.afreshPatternSplit(mFormListStr,"|") as String)
    }

    open fun patternKeyResult(hospitalStatu:String,formList:String){}
    open fun drugsNameResult(mDrugsList: String) {}

    override fun onDestroy() {
        super.onDestroy()
        if (mTitleArr != null) {
            mTitleArr?.clear()
            mTitleArr = null
        }
    }
}